<?php

/**
 * solution actions.
 *
 * @package    lezgro
 * @subpackage solution
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class solutionActions extends sfActions
{
    public function executeIndex(sfWebRequest $request) {
        $this->bg_class = 'bg_solution';
        $this->page_title = 'Lezgro - Solution';
        $this->getResponse()->setTitle($this->page_title);

        $solutions_on_page = sfConfig::get('app_solutions_count_on_page', 11);
        $this->pager = new sfDoctrinePager(
                        'Solution',
                        $solutions_on_page
        );
        //$this->pager->
        $this->pager->setQuery(SolutionTable::getI18NSolutionsToObjectQuery());
        $this->pager->setPage($request->getParameter('page', 1));
        $this->pager->init();
        if ($request->isXmlHttpRequest()) {
            $response = array();
            $response['html'] = $this->getPartial('index', array('pager' => $this->pager));
            $response['title'] = $this->page_title;
            $response['bg_class'] = $this->bg_class;
            return $this->renderText(json_encode($response));
        }
         $this->getResponse()->setTitle($this->page_title);
    }

    public function executeShowSolutionPortfolios(sfWebRequest $request) {
        $this->bg_class = 'bg_websites';
        $this->page_title = 'Lezgro - Solution - ';
        $this->solution = Doctrine_Core::getTable('Solution')->findOneBySlug($request->getParameter('slug'));
        $this->forward404Unless($this->solution);
        $this->page_title.=$this->solution->getSlug();
        if ($request->isXmlHttpRequest()) {
            $response = array();
            $response['html'] = $this->getPartial('showSolutionPortfolios', array('solution' => $this->solution));
            $response['title'] = $this->page_title;
            $response['bg_class'] = $this->bg_class;
            return $this->renderText(json_encode($response));
        }
         $this->getResponse()->setTitle($this->page_title);
    }
    public function executeShowAllSolutionPortfolios(sfWebRequest $request){
       $this->bg_class = 'bg_portfolio';
        $this->page_title = 'Lezgro - Solution - ';
        $this->pager = new sfDoctrinePager('Portfolio',11);
        $this->solution = Doctrine_Core::getTable('Solution')->findOneBySlug($request->getParameter('slug'));
        $this->forward404Unless($this->solution);
        $this->pager->setQuery(PortfolioTable::getPortfoliosObjectQuery($this->solution->getSlug()));
        $this->pager->setPage($request->getParameter('page', 1));
        $this->pager->init();
        $this->page_title.=$this->solution->getSlug();
        if ($request->isXmlHttpRequest()) {
            $response = array();
            $response['html'] = $this->getPartial('showAllSolutionPortfolios', array('pager' => $this->pager,'solution'=>$this->solution));
            $response['title'] = $this->page_title;
            $response['bg_class'] = $this->bg_class;
            return $this->renderText(json_encode($response));
        }
         $this->getResponse()->setTitle($this->page_title);
    }

  public function executeShowPortfolio(sfWebRequest $request)
  {
     $this->bg_class = 'bg_portfolio-web-no1';
        $this->page_title = 'Lezgro - Solution - ';
        $this->portfolio = Doctrine_Core::getTable('Portfolio')->findOneBySlug($request->getParameter('slug'));
        $this->previousPortfolio= PortfolioTable::getPrevious($this->portfolio->getId(),$this->portfolio->getSolutionId());
        $this->nextPortfolio= PortfolioTable::getNext($this->portfolio->getId(),$this->portfolio->getSolutionId());
        $this->forward404Unless($this->portfolio);
        $this->page_title.=$this->portfolio->getSlug();
        if ($request->isXmlHttpRequest()) {
            $response = array();
            $response['html'] = $this->getPartial('showPortfolio', array('portfolio' => $this->portfolio,
                'previousPortfolio'=>$this->previousPortfolio,'nextPortfolio'=>$this->nextPortfolio));
            $response['title'] = $this->page_title;
            $response['bg_class'] = $this->bg_class;
            return $this->renderText(json_encode($response));
        }
         $this->getResponse()->setTitle($this->page_title);
  }
  public function executeShowReviews(sfWebRequest $request) {
        $this->bg_class = 'bg_reviews';
        $this->page_title = 'Lezgro - Solution - ';
        $this->solution = Doctrine_Core::getTable('Solution')->findOneBySlug($request->getParameter('slug'));
        $this->forward404Unless($this->solution);
        $this->page_title.=$this->solution->getSlug();
        if ($request->isXmlHttpRequest()) {
            $response = array();
            $response['html'] = $this->getPartial('showReviews', array('solution' => $this->solution));
            $response['title'] = $this->page_title;
            $response['bg_class'] = $this->bg_class;
            return $this->renderText(json_encode($response));
        }
         $this->getResponse()->setTitle($this->page_title);
    }
    public function executeLoadOrderForm(sfWebRequest $request){
        sfContext::getInstance()->getConfiguration()->loadHelpers('I18N');
        $this->solution = Doctrine_Core::getTable('Solution')->findOneBySlug(array($request->getParameter('slug')));
        $this->forward404Unless($this->solution);
        $this->bg_class = 'bg_blog-comments';
        $this->page_title = 'Lezgro - Solution-Order';
        $this->form = new OrderForm();
        $this->form->setDefault('solution_id', $this->solution->getId());
        $this->form->setDefault('name', __('представтесь: '));
        $this->form->setDefault('organization', __('организация: '));
        $this->form->setDefault('email', __('e-mail: '));
        $this->form->setDefault('phone', __('телефон: '));
        $this->form->setDefault('description', __('Коментарии: '));
//        $this->form->setDefault('uri', __('www '));
//        $this->form->setDefault('type', __('интернет магазин: '));
//        if (sfContext::getInstance()->getUser()->getGuardUser()) {
//            $user = $this->getUser()->getUserProfile();
//            $this->form->setDefault('name', $user->getFirstName() . ' ' . $user->getLastName());
//            $this->form->setDefault('email', $user->getSfGuardUser()->getEmailAddress());
//        }
        if ($request->isXmlHttpRequest()) {
            $response = array();
            $response['html'] = $this->getPartial('orderForm', array('solution' => $this->solution, 'form' => $this->form));
            $response['title'] = $this->page_title;
            $response['bg_class'] = $this->bg_class;
            return $this->renderText(json_encode($response));
        }
        $this->getResponse()->setTitle($this->page_title);
    }
    public function executeCreateOrder(sfWebRequest $request) {
        $this->bg_class = 'bg_reviews';
        $this->page_title = 'Lezgro - Solution - ';
        if($request->isXmlHttpRequest()){
        $form = new OrderForm();
        sfContext::getInstance()->getConfiguration()->loadHelpers('URL');
         $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
                 $response=array('status'=>false);
        if ($form->isValid()) {
            $order = $form->save();
            $response['status']=true;
            $response['html']= $this->getPartial('order_success');
            $response['title'] = $this->page_title;
            $response['bg_class'] = $this->bg_class;
            $response['url']=  url_for('process_order_form_success');
        }else{
            foreach($form->getErrorSchema()->getErrors() as $error)
            $response['errors'][$error->getMessageFormat() ]=$error->getValue() ;
        }
        return $this->renderText(json_encode($response));
        }
        $this->getResponse()->setTitle($this->page_title);
    }
    public function executeSuccessOrder(sfWebRequest $request){
        $this->bg_class = 'bg_reviews';
        $this->page_title = 'Lezgro - Solution - ';
        if($request->isXmlHttpRequest()){
       
            $response['status']=true;
            $response['html']= $this->getPartial('order_success');
            $response['title'] = $this->page_title;
            $response['bg_class'] = $this->bg_class;
            $response['url']=  url_for('process_order_form');
        return $this->renderText(json_encode($response));
        }
        $this->getResponse()->setTitle($this->page_title);
    }
  
}
